<?php
defined('IN_IA') or exit('Access Denied');
$op         = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$uniacid    = intval($_W['uniacid']);
$check_code    = $_GPC['link_url'];

$merchantid = MERCHANTID ? MERCHANTID : $_GPC['merchantid'];

if($op=='checking' && $_W['isajax']){
	if(empty($check_code)){
		die(json_encode(array("errcode" => 1,'errmsg'=>'请输入核销码')));
	}
	if(strlen(trim($check_code)) == 8){	//核销码
		$where['uniacid'] = $uniacid;
		$where['hexiaoma'] = addslashes($check_code);
	}else{	//核销链接
		$parse = parse_url($check_code);
		if(empty($parse['query'])){
			die(json_encode(array("errcode" => 1,'errmsg'=>'二维码错误-01')));
		}
		parse_str($parse['query'],$query);
		if(empty($query['mid'])){
			die(json_encode(array("errcode" => 1,'errmsg'=>'二维码错误-02')));
		}
		$where['uniacid'] = $uniacid;
		$where['orderno'] = $query['mid'];
	}
	$fields = 'id,activityid,orderno,nickname,realname,mobile,jointime,status,hexiaoma,review,status';
	$records = pdo_get('aide_activity_records', $where);
	if(!$records){
		die(json_encode(array("errcode" => 1,'errmsg'=>'二维码错误-03')));
	}
	if($records['ishexiao'] == 1){
		die(json_encode(array("errcode" => 1,'errmsg'=>'该票据已核销，请勿重复操作！')));
	}
	if($records['review'] <> 1){
		die(json_encode(array("errcode" => 1,'errmsg'=>'该票据未通过审核！')));
	}
	if($records['status'] == 5){
		die(json_encode(array("errcode" => 1,'errmsg'=>'该票据已取消报名！')));
	}
	$form_data_common = pdo_get('aide_form_data_common', array('rid'=>$records['id']));
	$form_data = pdo_getall('aide_form_data', array('recordid'=>$records['id']));
	foreach($form_data as $k=>&$v){
		$form = pdo_get('aide_form', array('id'=>$v['formid']));
		$v['title'] = $form['title'];
	}
	$records['form_data_common'] = $form_data_common;
	$records['form_data'] = array_reverse($form_data);

	$record_data['ishexiao'] = 1;
	$record_data['status'] = 3;
	$record_data['sendtime'] = date("Y-m-d H:i:s");
	$res = pdo_update('aide_activity_records',$record_data,array('id'=>$records['id']));
	if(!$res){
		die(json_encode(array("errcode" => 1,'errmsg'=>'核销失败')));
	}

	die(json_encode(array("errcode" => 0,'errmsg'=>'核销成功','data'=>$records)));
}

if($op == 'display'){
	include aide_template ('checking');
}
